package pl.plesken.graphic;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import org.apache.xmlbeans.XmlException;

import pl.plesken.xml.EditXMLFile;

public class CreateUniqueBGLImage {

	String file;
	String img;
	
	public void generateImage(String dir, String baseName) {
		try {
			GraphicTools cg = new GraphicTools();
			EditXMLFile xml = new EditXMLFile();
			
			file = dir + File.separator + baseName + ".xml";
			img = dir + File.separator + baseName + ".jpg";
			String thumbnailExt = xml.thumbnailFormat(xml.loadData(file));
			String posterExt = xml.postersFormat(xml.loadData(file));
			int largeWidth = Integer.valueOf(xml.posterWidth(xml.loadData(file)));
			int largeHeight = Integer.valueOf(xml.posterHeight(xml.loadData(file)));
			int smallWidth = Integer.valueOf(xml.thumbnailWidth(xml.loadData(file)));
			int smallHeight = Integer.valueOf(xml.thumbnailHeight(xml.loadData(file)));
			
			BufferedImage bi = cg.loadImage(img);
			BufferedImage bg_large = cg.loadImage("pictures/BoxImages/Box_Detail.png");
			BufferedImage bg_small = cg.loadImage("pictures/BoxImages/Box_Single.png");
			BufferedImage kind = cg.loadImage("pictures/BoxImages/BluRay.png");
			//BufferedImage rHD = cg.loadImage("pictures/Cover/rc_hd1080.png");
			BufferedImage mask_large = cg.createMask(bg_large.getWidth(), bg_large.getHeight());
			BufferedImage mask_small = cg.createMask(bg_small.getWidth(), bg_small.getHeight());
			
			String largeOutput = dir + File.separator + baseName + "_large." + posterExt;
			String smallOutput = dir + File.separator + baseName + "_small." + thumbnailExt;
			
			BufferedImage largeIMG = cg.resizeImage(bi, 300, 428);
			BufferedImage smallIMG = cg.resizeImage(bi, 175, 258);
			
			largeIMG = cg.addSetImage(largeIMG, mask_large, 45, 7);
			largeIMG = cg.addSetImage(bg_large, largeIMG, 1, 1);
			
			smallIMG = cg.addSetImage(smallIMG, mask_small, 30, 13);
			smallIMG = cg.addSetImage(bg_small, smallIMG, 1, 1);
			smallIMG = cg.addSetImage(kind, smallIMG, 214, 169);
			
			cg.saveImageAsPng(largeIMG, "/home/plesken/test.png");
			
		} catch(IOException e) {
			e.printStackTrace();
		} catch(XmlException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		CreateUniqueBGLImage g = new CreateUniqueBGLImage();
		g.generateImage("/home/plesken/Wideo/a/Jukebox", "Friday the 13th Killer Cut 2009 1080p BluRay DTS x264-DON");
	}
}
